Skip to content

Rewrite as a real packet sniffer: framed stream, kernel BPF, pcap on both ends - #1

Merged
bas3line merged 3 commits into
mainfrom
rewrite/pktsniff-2
Nov 15, 2026
Merged

bas3line merged 3 commits into
mainfrom
rewrite/pktsniff-2

Conversation

@bas3line

Copy link
Copy Markdown
Owner

The old sniffer/ sent pipe-delimited text lines over TCP. Nothing downstream could parse them, the client fell out of sync whenever a line straddled a recv(), the hexdump was cut at 64 bytes and squashed onto one line, and there were no filters and no .pcap, so the output was only useful if you happened to be staring at it.

This replaces it with a project that is meant to be used: include/, src/, tests/, docs/, scripts/, packaging/, a Makefile with real targets, unit tests, an e2e suite, CI, a systemd unit, and a README in the same shape as ascii.rest and nodeny.

What it does now

  • Binary wire protocol (docs/PROTOCOL.md) — length-prefixed frames with magic/version: HELLO, PACKET, STATS, BYE, ERR. The client has an incremental reader, so a frame split across TCP segments cannot break it.
  • Capture — AF_PACKET with promiscuous mode, kernel BPF compiled through libpcap (pcap_compile → SO_ATTACH_FILTER, optional at build time), SO_TIMESTAMPNS kernel timestamps, PACKET_STATISTICS drop accounting. The control connection is excluded from the capture by default so streaming over lo does not feed back on itself.
  • Server — one epoll loop, non-blocking fan-out to N clients, and a per-client queue that only ever drops frames with no bytes on the wire yet. Dropping a partly-sent frame is precisely what desynchronised the old client under load.
  • Dissection — Ethernet/VLAN/QinQ, Linux SLL, BSD loopback, raw IP; ARP, IPv4, IPv6 with extension headers; TCP (flags, seq/ack, MSS), UDP, ICMP, ICMPv6; plus DNS names, TLS SNI and HTTP request lines. Every field bounds-checked.
  • Client — display filter with or/|, ! negation and CIDR; hexdump; NDJSON for jq; .pcap export; exact --count/--duration; reconnect with backoff; colour only on a tty.
  • .pcap on both ends, with size rotation, readable by tcpdump/Wireshark/tshark.

Two real bugs found while testing

  1. The queue dropped frames while the send cursor was mid-frame — that is the desync. Fixed by tracking frame boundaries separately from the send cursor, and never dropping a frame that is partly on the wire.
  2. ps_outbuf_push gave up as soon as one drop failed, even after emptying the queue, leaving the queue permanently wedged at "full". Both are now regression-tested.

Verified

  • make test — 200+ checks: framing fed one byte at a time, dissection cut at every truncation offset, filter grammar, and the send queue under short writes and a stuck reader (re-parses the delivered byte stream to prove every frame arrives whole and in order).
  • make asan — clean under ASan + UBSan.
  • sudo tests/e2e.sh — 25 live checks on loopback, all green: .pcap re-read by tcpdump, BPF filtering, a 64 KiB client buffer with no desync, JSON validity, --count exactness, client rejection at --max-clients, clean SIGTERM shutdown.
  • Soak: 771k frames / 713 MB, 63k frames dropped for a deliberately slow client, zero desync, zero sanitizer reports.
  • Builds warning-free with -Werror; also builds without libpcap, where --filter fails at startup with a message that explains why instead of being silently ignored.

The old code sent pipe-delimited text lines over TCP: nothing downstream
could parse them, the client fell out of sync when a line straddled a recv(),
and there was no filter, no .pcap, no back pressure.

Now:

* Binary wire protocol (docs/PROTOCOL.md): length-prefixed frames, magic +
  version, HELLO/PACKET/STATS/BYE/ERR. The incremental reader handles TCP
  segmentation, so the client cannot break on a split frame.
* Capture: AF_PACKET with promiscuous mode, kernel BPF filters compiled with
  libpcap (optional at build time), kernel receive timestamps, drop
  accounting, and the control connection excluded from the capture by default
  so streaming over a captured interface does not feed back on itself.
* Server: single epoll loop, non-blocking fan-out to N clients, per-client
  frame-atomic queue that only ever drops wholly-unsent frames (dropping a
  partly-sent frame is what desynchronised clients under load).
* Dissection: Ethernet/VLAN/QinQ, SLL, BSD loopback, raw IP; ARP, IPv4, IPv6
  with extension headers; TCP (flags/seq/ack/MSS), UDP, ICMP, ICMPv6; plus DNS
  names, TLS SNI and HTTP request lines. Every field is bounds-checked.
* Client: structured display filter with AND/OR/negation and CIDR, hexdump,
  NDJSON for jq, .pcap export, exact --count/--duration, reconnect with
  backoff, colour auto-detection.
* .pcap output on both ends (Wireshark-openable), with size rotation.
* Proper project layout: include/, src/, tests/, docs/, scripts/, packaging/;
  Makefile with unit/asan/install targets; systemd unit; CI.
* Tests: unit tests for framing, dissection (every truncation offset), filter
  grammar and the send queue under partial writes and drops; plus a live
  root-only e2e suite. Verified clean under ASan/UBSan and in a 700 MB/s
  soak with 63k dropped frames and no desync.
Same shape as ascii.rest and nodeny: banner from ascii.rest, badges, a
one-line pitch, a real transcript, then Why / Install / Run it, and option
tables instead of walls of flags. Adds CONTRIBUTING.md with the rules that
actually matter here (never trust a length, keep the stream frame-atomic,
server stays dumb) and points every link at a file that exists.
Copilot AI balanced review requested due to automatic review settings November 15, 2026 01:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

CI caught this: excluding the stream's own connection synthesises a BPF
expression, so a build without libpcap could not open the capture at all and
every run died at startup. Only synthesise the exclusion when we can compile
it, and note the consequence at debug level otherwise.

e2e.sh now detects libpcap and skips the kernel-filter checks instead of
failing, so it can run in both CI matrix legs.
@bas3line
bas3line merged commit 87939db into main Nov 15, 2026
8 checks passed
bas3line added a commit that referenced this pull request Nov 15, 2026
Rewrite as a real packet sniffer: framed stream, kernel BPF, pcap on both ends
bas3line added a commit that referenced this pull request Nov 15, 2026
Rewrite as a real packet sniffer: framed stream, kernel BPF, pcap on both ends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants